package org.softmed.rest.admin.httpMethods;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;

import org.softmed.rest.admin.DetailChooser;
import org.softmed.rest.admin.DetailLevelListener;
import org.softmed.rest.admin.DetailChooser.DetailLevel;
import org.softmed.rest.admin.interfaces.EventListener;
import org.softmed.swing.IconManager;

public class HTTPMethodHeader extends JPanel implements ActionListener,
		DetailLevelListener {

	JLabel methodName = new JLabel();
	JLabel availableTypes = new JLabel();
	JButton add = new JButton(IconManager.add);
	EventListener listener;

	DetailChooser detailChooser = new DetailChooser(true, DetailLevel.METHODS,
			DetailLevel.MEDIA_TYPE);

	int width = 20;
	int height = 20;

	public HTTPMethodHeader() {
//		 setBackground(Color.ORANGE);

		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));

		setPreferredSize(new Dimension(540, 25));
		setMaximumSize(new Dimension(540, 25));
		setMinimumSize(new Dimension(540, 25));

		methodName.setPreferredSize(new Dimension(40, 25));
		methodName.setMaximumSize(new Dimension(40, 25));
		methodName.setMinimumSize(new Dimension(40, 25));

		// availableTypes.setPreferredSize(new Dimension(500,25));
		// availableTypes.setMaximumSize(new Dimension(500,25));
		// availableTypes.setMinimumSize(new Dimension(500,25));

		methodName.setPreferredSize(new Dimension(100, 20));

		availableTypes.setPreferredSize(new Dimension(250, 20));

		add(methodName);
		// add(availableTypes);
		addButton(add);

		add(detailChooser);

		add(Box.createHorizontalGlue());
		// add(remove);

		// remove.addActionListener(this);
		// add.addActionListener(this);
		add.setEnabled(true);
	}

	public void setMethodName(String text) {
		methodName.setText(text);
	}

	public void setMediaNames(List<String> types) {
		String test = "";

		for (String string : types) {
			test += string + ", ";
		}

		if (test.length() > 0)
			test = test.substring(0, test.lastIndexOf(','));

		methodName.setText(test);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		Object source = arg0.getSource();
		if (source == add) {
			listener.listen(this);
		}

	}

	public EventListener getListener() {
		return listener;
	}

	public void setListener(EventListener listener) {
		this.listener = listener;
	}

	protected void addButton(AbstractButton handlers2) {
		resizeButton(handlers2, width, height);
		handlers2.addActionListener(this);
		handlers2.setEnabled(false);
		add(handlers2);
	}

	protected void resizeButton(AbstractButton button, int width, int height) {
		button.setPreferredSize(new Dimension(width, height));
		button.setMaximumSize(new Dimension(width, height));
		button.setMinimumSize(new Dimension(width, height));
	}

	@Override
	public void detailLevelChanged(DetailLevel detailLevel) {
		detailChooser.setDetailLevel(detailLevel);
	}

	public void setDetailListener(DetailLevelListener listener) {
		detailChooser.setDetailListener(listener);
	}
}
